package com.stefanini.proyecto.controladores;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import com.fifa.filters.SecurityFilter;
import com.stefanini.persistencia.Usuario;
import ejbs.UserManager;
import java.io.IOException;
import java.io.Serializable;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.servlet.http.HttpSession;


/**
 *
 * @author EDWIN
 */

@Named(value = "loginAdminController")
@RequestScoped
public class LoginAdminController implements Serializable {
    
    private String usuario;
    private String password;
    
    @EJB
    private UserManager userManager;
    
    public void login() {
        Usuario user = userManager.verificarLogin(usuario, password);

        FacesContext ctx = FacesContext.getCurrentInstance();

        if (user == null) {
            
            String message = ResourceBundle.getBundle("com.fifa.messages", new Locale("es")).getString("login.error.loginFailed");
            ctx.addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error",
                            message));
        } else {
            HttpSession session = (HttpSession) ctx.getExternalContext().getSession(true);
            session.setAttribute(SecurityFilter.LOGGED_USER, user.getUsername());
            session.setAttribute(SecurityFilter.IS_LOGGED_IN, Boolean.TRUE);

            try {
                String url = ResourceBundle.getBundle("com.fifa.urls").getString("index.url");
                ctx.getExternalContext().redirect(url);
            } catch (IOException ex) {
                Logger.getLogger(LoginAdminController.class.getName()).log(Level.SEVERE, "Error al redireccionar", ex);
            }
        }
    }
    
    public void logout() {
        FacesContext ctx = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) ctx.getExternalContext().getSession(false);
        session.invalidate();

        try {
            ctx.getExternalContext().redirect("/Proyecto/login.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(LoginAdminController.class.getName()).log(Level.SEVERE, "Error al redireccionar", ex);            
        }

    }    
    
    public void changeLanguage() {
        if (FacesContext.getCurrentInstance()
                .getViewRoot().getLocale().equals(new Locale("es"))) {
            FacesContext.getCurrentInstance()
                    .getViewRoot().setLocale(Locale.ENGLISH);
        } else {
            FacesContext.getCurrentInstance()
                    .getViewRoot().setLocale(new Locale("es"));
        }
    }
    
    

    /**
     * @return the usuario
     */
    public String getUsuario() {
        return usuario;
    }

    /**
     * @param usuario the usuario to set
     */
    public void setUsuario(String usuario) {
        this.usuario = usuario;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }
    
    public void setPassword(String password) {
        this.password = password;
    }
}
